package com.neo.controller;

import com.neo.bean.UserManagement;
import com.neo.common.base.AppConstants;
import com.neo.common.base.Base2Controller;
import com.neo.common.utils.JuHeUtils;
import com.neo.common.utils.StringUtils;
import com.neo.common.utils.UUIDGen;
import com.neo.common.utils.jedis.JedisClient;
import com.neo.mapper.UserManagementMapper;
import com.neo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@Api(value = "公共接口", description = "公共接口", tags = "common")
@RestController
@RequestMapping("/api/common")
public class CommonController extends Base2Controller {

    @Autowired
    private UserManagementMapper userManagementMapper;
    /**
     * 初始化登录信息
     *
     * @param
     * @param adminMamger 管理员信息
     * @param
     * @return
     */
    public UserManagement initLoginAdminMamgerInfo(UserManagement adminMamger) {
        // 获取存入redis 里面的sessionId  "" null
        String sessionId = jedisClient.get(AppConstants.USER_PRE + adminMamger.getId());
        // 如果不为空的时候删掉存入的值
        if (StringUtils.isNotBlank(sessionId)) {
            jedisClient.del(AppConstants.USER_PRE + adminMamger.getId());
            jedisClient.del(sessionId);
            jedisClient.del(AppConstants.USER_INFO + sessionId);
        }
        sessionId = UUIDGen.genUUID();
        adminMamger.setToken(sessionId);
        userManagementMapper.updateByPrimaryKeySelective(adminMamger);
        // 重新存入sessionId
        jedisClient.set(AppConstants.USER_PRE + adminMamger.getId(), sessionId);
        // 设置过期时间为十分钟
        // jedisClient.expire(AppConstants.USER_PRE + adminMamger.getEmployeeFiles().getEmployeeId(), 600);
        jedisClient.set(sessionId, adminMamger.getTelPhone());
        JSONObject jsonObject = JSONObject.fromObject(adminMamger);
        jedisClient.set(AppConstants.USER_INFO + sessionId, jsonObject.toString());
        return adminMamger;
    }


}